require 'rake'
require 'rake/testtask'
require 'rake/clean'
require 'rake/gempackagetask'
require 'rake/rdoctask'
require 'tools/rakehelp'
require 'fileutils'
include FileUtils

setup_tests

setup_clean ["pkg", "lib/*.bundle", "*.gem", "doc/site/output", ".config"]

setup_rdoc ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc']

desc "Compile and test"
task :default => [:test]

task :package => [:clean,:test,:rerdoc]

task :ragel_ruby do
  version = `ragel --version`.scan(/version ([0-9\.]+)/).first.first
  if version >= "5.24"
    sh 'ragel -R esi-ragel.rl | rlgen-ruby > esi-ragel.rb'
  else
    puts "You need to install a version of ragel greater or equal to 5.24"
    exit(1)
  end
end
task :ragel_graph do
  sh 'ragel -R esi-ragel.rl | rlgen-dot -p > esi-ragel.dot'
  sh 'dot -Tpng esi-ragel.dot -o esi-ragel.png'
  if RUBY_PLATFORM.match /darwin/
    sh 'open esi-ragel.png'
  end
end
task :ragel_test => :ragel_ruby do
  sh 'ruby esi-ragel.rb'
end

name="mongrel-esi"
version="0.2.0"

setup_gem(name, version) do |spec|
  spec.summary = "A small fast ESI HTTP Server built on top of Mongrel"
  spec.description = spec.summary
  spec.test_files = Dir.glob('test/test_*.rb')
  spec.author="Todd A. Fisher"
  spec.executables=['mongrel_esi']
  spec.files += %w(COPYING LICENSE README Rakefile setup.rb)

  spec.required_ruby_version = '>= 1.8.4'

  if RUBY_PLATFORM =~ /mswin/
    spec.platform = Gem::Platform::WIN32
  else
    spec.add_dependency('daemons', '>= 1.0.3')
    spec.add_dependency('fastthread', '>= 0.6.2')
  end
  
  spec.add_dependency('hpricot', '>= 0.6')
  spec.add_dependency('memcache-client', '>= 1.5.0')
  spec.add_dependency('gem_plugin', '>= 0.2.2')
  spec.add_dependency('cgi_multipart_eof_fix', '>= 1.0.0')
end

task :install do
  sub_project("gem_plugin", :install)
  sub_project("fastthread", :install)
  sh %{rake package}
  sh %{gem install pkg/mongrel-esi-#{version}}
  if RUBY_PLATFORM =~ /mswin/
    sub_project("mongrel_service", :install)
  end
end

task :uninstall => [:clean] do
  sh %{gem uninstall mongrel-esi}
  sub_project("gem_plugin", :uninstall)
  sub_project("fastthread", :uninstall)
  if RUBY_PLATFORM =~ /mswin/
    sub_project("mongrel_service", :install)
  end
end


task :gem_source do
  mkdir_p "pkg/gems"
 
  FileList["**/*.gem"].each { |gem| mv gem, "pkg/gems" }
  FileList["pkg/*.tgz"].each {|tgz| rm tgz }
  rm_rf "pkg/#{name}-#{version}"

  sh %{ index_gem_repository.rb -d pkg }
  # TODO: setup something like this
  #sh %{ scp -r ChangeLog pkg/* rubyforge.org:/var/www/gforge-projects/mongrel/releases/ }
end
